package com.web.servlet.config;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashMap;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.xml.internal.xsom.impl.scd.Iterators.Map;

/**
 * 加载配置，若需要增加配置：
 * <p/>1.在config.properties中增加属性name
 * <p/>2.在ConfigBean中添加属性name
 * <p/>3.config.properties和ConfigBean中的属性名称必须一致
 * <p/>4.使用配置使用Config.bean.name即可
 * @author marvin
 * 
 */
public class Config {
	public static final Logger log = LoggerFactory.getLogger(Config.class);
	public static final HashMap<String, String> dbConfigMap = new HashMap<String, String>();
	public static final HashMap<String, String> redisConfigMap = new HashMap<String, String>();
	
	private static boolean isLoaded = false;

	private Config() {}
	
	public static void init(String classpath){
		synchronized (Config.class) {
			if(!isLoaded){
				//loadConfig();
				loadDBConfig(classpath);
				loadRedisConfig(classpath);
				isLoaded = true;
			}
		}
	}
	/**
	 * 读取指定位置配置文件
	 * @param path
	 * @return
	 */
	private static Properties loadConfig(String path){
		//String dbPath = "/game/server/app/castleserver/config/db.properties";
		InputStream fileinputstream = null;
		try {
			fileinputstream = new FileInputStream(path);
		} catch (Exception e) {
			String msg = String.format("未找到DB配置文件%s", path);
			System.out.println(msg);
			log.error(msg, e);
		}
		Properties dbProps = new Properties();
		try {
			dbProps.load(fileinputstream);
		} catch (Exception e) {
			String msg = String.format("打开文件%s出错", path);
			log.error(msg, e);
		}
		return dbProps;
	}
	private static void loadDBConfig(String classpath){
		String dbPath = classpath+"/db.properties";
		Properties dbProps = loadConfig(dbPath);
		String url = dbProps.getProperty("mysql.url");
		String user = dbProps.getProperty("mysql.user");
		String password = dbProps.getProperty("mysql.password");
		String maxconn = dbProps.getProperty("mysql.maxconn");
		String fallow = dbProps.getProperty("mysql.fallow");
		System.out.println("Config.loadDBConfig():"+url);
		dbConfigMap.put("url", url);
		dbConfigMap.put("user", user);
		dbConfigMap.put("password", password);
		dbConfigMap.put("maxconn", maxconn);
		dbConfigMap.put("fallow", fallow);
	}
	private static void loadRedisConfig(String classpath){
		String dbPath = classpath+"/redis.properties";
		Properties dbProps = loadConfig(dbPath);
		String url = dbProps.getProperty("redis.url");
		String user = dbProps.getProperty("redis.user");
		String password = dbProps.getProperty("redis.password");
		String port = dbProps.getProperty("redis.port");
		String poolsize = dbProps.getProperty("redis.poolsize");
		System.out.println("Config.loadRedisConfig():"+url);
		redisConfigMap.put("url", url);
		redisConfigMap.put("user", user);
		redisConfigMap.put("password", password);
		redisConfigMap.put("port", port);
		redisConfigMap.put("poolsize", poolsize);
	}
	/*private static void loadConfig(){
		try {
			InputStream is = ClassLoader.getSystemResourceAsStream("config.properties");
			Properties p = new Properties();
			p.load(is);
			Class<?> clazz = bean.getClass();
			Field[] fields = clazz.getDeclaredFields();
			for(Field f : fields){
				String v = p.getProperty(f.getName());
				log.debug("getProperty:"+f.getName()+" value :"+v);
				if(v.trim().isEmpty())
					continue;
				f.setAccessible(true);
				f.set(bean, convertType(v, f));
			}
			log.debug("bean:"+bean.SERVER_ID);
			p.clear();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	private static Object convertType(String v, Field f) {
		String s = f.getType().getSimpleName();
		if (s.equals("String")) {
			return v;
		} else if (s.equals("int") || s.equals("Integer")) {
			return Integer.parseInt(v);
		} else if (s.equals("boolean") || s.equals("Boolean")) {
			return Boolean.parseBoolean(v);
		} else if (s.equals("String[]")) {
			return v.split(",");
		} else {
			throw new IllegalArgumentException("field type is invalid.");
		}
	}
}
